package luohuayu.CatPixelmonSync;

import java.util.Optional;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;

import com.pixelmonmod.pixelmon.Pixelmon;
import com.pixelmonmod.pixelmon.api.economy.IPixelmonBankAccount;
import com.pixelmonmod.pixelmon.api.economy.IPixelmonBankAccountManager;
import com.pixelmonmod.pixelmon.api.events.EconomyEvent.PostTransactionEvent;
import com.pixelmonmod.pixelmon.api.events.EconomyEvent.PreTransactionEvent;
import com.pixelmonmod.pixelmon.api.events.EconomyEvent.transactionType;
import com.pixelmonmod.pixelmon.storage.PlayerStorage;

import net.milkbowl.vault.Vault;
import net.milkbowl.vault.economy.Economy;

public class CatPixelmonSync extends JavaPlugin{
    public static CatPixelmonSync instance;
    public Pixelmon pixelmon;
    public IPixelmonBankAccountManager moneyManager;

    public void onLoad() {
        instance = this;
        try {
            setupPixelmon();
            setupEconomy();
        } catch (Exception e) {
            e.printStackTrace();
            Bukkit.getPluginManager().disablePlugin(this);
        }
    }

    private void setupPixelmon() throws Exception {
        pixelmon = Pixelmon.instance;
        moneyManager = Pixelmon.moneyManager;
    }

    private void setupEconomy() throws Exception {
        Vault vault = (Vault) Bukkit.getPluginManager().getPlugin("Vault");
        Bukkit.getServicesManager().register(Economy.class, new EconomyHook(), vault, ServicePriority.Highest);
    }

    public IPixelmonBankAccount getPixelmonBankAccount(String playerName) {
        UUID uuid = Bukkit.getOfflinePlayer(playerName).getUniqueId();
        return getPixelmonBankAccount(uuid);
    }

    public IPixelmonBankAccount getPixelmonBankAccount(UUID uuid) {
        Optional<? extends IPixelmonBankAccount> account = moneyManager.getBankAccount(uuid);
        return account.isPresent() ? account.get() : null;
    }

    public boolean changeMoney(IPixelmonBankAccount account, int change) {
        PlayerStorage ps = (PlayerStorage) account;
        int oldBalance = ps.pokeDollars;
        transactionType type = change < 0 ? transactionType.withdraw:transactionType.deposit;
        PreTransactionEvent preEvent = new PreTransactionEvent(ps.getPlayer(), type, ps.pokeDollars, change);
        if(!Pixelmon.EVENT_BUS.post(preEvent)) {
            int newBalance = ps.pokeDollars + preEvent.change;
            if(ps.pokeDollars != newBalance) {
                ps.pokeDollars = newBalance;
                if(ps.pokeDollars > 999999) {
                    ps.pokeDollars = 999999;
                } else if(ps.pokeDollars < 0) {
                    ps.pokeDollars = 0;
                }

                if (ps.getPlayer() != null) ps.updatePlayer(ps.pokeDollars);
            }
            Pixelmon.EVENT_BUS.post(new PostTransactionEvent(ps.getPlayer(), type, oldBalance, ps.pokeDollars));
        } else {
            return false;
        }

        return true;
    }
}
